package com.taotao.service.impl;

import com.taotao.HttpUtil;
import com.taotao.SystemConstants;
import com.taotao.TaotaoResult;
import com.taotao.pojo.SearchResult;
import com.taotao.service.SearchService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author evader
 * @date 2020-09-30 14:36
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Value("${search.url}")
    private String searchUrl;

    @Override
    public SearchResult search(String keywords, Integer pageNum, Integer pageSize) {
        Map<String, String> params = new HashMap<>();
        params.put("q", keywords);
        params.put("pageNum", pageNum+"");
        params.put("pageSize", pageSize+"");
        String searchResultJson = HttpUtil.doPost(searchUrl + "/search", params);
        TaotaoResult taotaoResult = TaotaoResult.formatToPojo(searchResultJson, SearchResult.class);
        if(taotaoResult.getStatus().equals(SystemConstants.TAOTAO_RESULT_STATUS_OK)) {
            return (SearchResult) taotaoResult.getData();
        }
        return null;
    }
}

